package br.com.poc.pitagorasnumero.util;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import br.com.poc.pitagorasnumero.R;

/**
 * @author irlan.gomes
 * 
 */
public final class PitagorasUtil {

	/**
	 * 
	 */
	private PitagorasUtil() {
	}

	/**
	 * Method to verify if there's internet connection
	 * 
	 */
	public static boolean isInternetConnected(final Context context) {
		boolean status = false;
		if (context != null) {
			final ConnectivityManager connMgr = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			if (connMgr != null && connMgr.getActiveNetworkInfo() != null) {
				status = connMgr.getActiveNetworkInfo() == null ? false
						: connMgr.getActiveNetworkInfo()
								.isConnectedOrConnecting();

			}

		}

		return status;
	}

	/**
	 * Show Toast
	 * 
	 * @param context
	 * @param msgId
	 */
	public static void showToast(final Context context, final int msgId) {

		if (context instanceof Activity) {
			showCustomToast((Activity) context, msgId);
		}
	}

	/**
	 * Show a custom toast
	 * 
	 * @param activity
	 * @param id
	 */
	private static void showCustomToast(final Activity activity, final int msgId) {

		final LayoutInflater inflater = activity.getLayoutInflater();

		final View toastLayout = inflater.inflate(R.layout.pitagoras_toast,
				(ViewGroup) activity.findViewById(R.id.toast_layout_root));
		final TextView txv = (TextView) toastLayout.findViewById(R.id.text);
		txv.setText(activity.getResources().getString(msgId));
		txv.setGravity(Gravity.CENTER_HORIZONTAL);

		final Toast toast = new Toast(activity);
		toast.setView(toastLayout);
		toast.setDuration(Toast.LENGTH_SHORT);
		toast.show();

	}

}
